import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import java.io.File;
import java.util.Set;
import java.util.concurrent.TimeUnit;

public class JD {


    public static void main(String[] args) throws Exception {
        String filePath = System.getProperty("user.dir") + File.separator + "program" + File.separator + "chromedriver.exe";
        System.setProperty("webdriver.chrome.driver", filePath);
        WebDriver driver = new ChromeDriver();
        //Actions action = new Actions(driver);
        //打开京东主页
        driver.get("https://www.jd.com/");
        Thread.sleep(2000);

        //登录按钮
        driver.findElement(By.className("link-login")).click();
        Thread.sleep(3000);

        //选择QQ登陆
        driver.findElement(By.className("pdl")).click();
        Thread.sleep(3000);

        //switcher_plogin
        String home = driver.getWindowHandle();
        Set<String> handles = driver.getWindowHandles();
        for (String handle : handles) {
            if(!handle.equals(home)){
                driver.switchTo().window(handle);
                Thread.sleep(1000);
                //最大化
                driver.manage().window().maximize();
                Thread.sleep(1000);
            }
        }

        driver.switchTo().frame("ptlogin_iframe");//切入页面iframe
        Thread.sleep(1000);
        driver.findElement(By.id("img_out_1091569752")).click();
        Thread.sleep(2000);


        //点击购物车主页
        driver.get("https://cart.jd.com/cart.action");
        Thread.sleep(2000);


        //京东购物车默认都是全选的了，直接点击结算按钮
        //driver.findElement(By.xpath("//div[@id='cart-floatbar']/div/div/div/div[2]/div[4]/div[1]/div/div[1]")).click();

        //由于京东现在做了脚本校验，无法点击，直接请求接口路径算了！！！
        driver.get("https://trade.jd.com/shopping/order/getOrderInfo.action");
        Thread.sleep(2000);
        //提交订单
        driver.findElement(By.id("order-submit")).click();
        Thread.sleep(2000);

        try {
            driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}
